<?php
include_once('_path.php');
#db
include_once(_CONST_LOCAL_INCLUDE_PATH."database.class.php");
$database = new Database($db_vars);

if ($db_slave_vars['hostname'] != '') {
	$slave = new Database($db_slave_vars);
} else {
	$slave =& $database;
}

#通用函数
include_once(_CONST_LOCAL_INCLUDE_PATH."common.php");
#处理缩略图
include_once(_CONST_LOCAL_INCLUDE_PATH.'phpthumb/ThumbLib.inc.php');
#处理淘宝客
include_once(_CONST_LOCAL_INCLUDE_PATH."/top/RequestCheckUtil.php");
include_once(_CONST_LOCAL_INCLUDE_PATH."/top/ItemGetRequest.php");
include_once(_CONST_LOCAL_INCLUDE_PATH."/top/TaobaokeItemsConvertRequest.php");

#登录权限
check_loggedin();

#处理上一步骤
	
$token = $_POST['token'];
if(empty($token) || $token != $_SESSION['token']){
	header("Location:"._CONST_WEB_URL."flow-1.php");
	exit;
}

$images = $_SESSION['step_1']['gimg'];
$uploadpath = _CONST_PIC_PATH . 'vote/' . $_SESSION['userid'] . "/";
if(!is_dir($uploadpath)) {
	@mkdir($uploadpath);
}
$basename = md5(_CONST_TIMENOW) . '.jpg';
$uploadfile = $uploadpath . $basename;
$imgc = file_get_contents($images);
file_put_contents($uploadfile, $imgc);
$options = array('resizeUp' => true, 'jpegQuality' => 100);
$thumb1 = PhpThumbFactory::create($uploadfile, $options);
$thumb1->adaptiveResize(450, 450)->save($uploadpath . '2_' . $basename);
$thumb2 = PhpThumbFactory::create($uploadfile, $options);
$thumb2->resize(300, 10000)->save($uploadpath . '1_' . $basename);

#生成投票
$url = $_SESSION['step_1']['gurl'];
$title = $_SESSION['step_1']['title'];
$desc = $_SESSION['step_3']['desc'];
$user_id = $_SESSION['userid'];
$thumbfile = $_SESSION['step_2']['myimg'];
$thumbfile = substr($thumbfile, strrpos($thumbfile, '/')+3); 

$create_time = _CONST_TIMENOW;

#taobaoke url
$array = array(
'item.taobao.com',
'detail.tmall.com'
);
foreach($array as $u)
{
	if(preg_match("#$u#i", $url))
	{
		if(preg_match("#id=([0-9]+)#i", $url, $match))
		{
			$c = new TopClient;
			$c->format = 'json';
			$c->appkey = TB_AKEY;
			$c->secretKey = TB_SKEY;
			$req = new TaobaokeItemsConvertRequest;
			$req->setFields("click_url,num_iid");
			$req->setNumIids($match[1]);
			$req->setPid(TBK_PID);
			$resp = $c->execute($req);
			if(!empty($resp->total_results>0))
			{
				$url = $resp->taobaoke_items->taobaoke_item[0]->click_url;
			}
		}
	}	
}

$database->execute("INSERT INTO vote (title, url, `desc`, user_id, images, thumbfile, create_time) VALUES (".
                    string_sql($title). " ,".
					string_sql($url). " ,". 
					string_sql($desc). " ,". 
					$_SESSION['userid'] . " ,".
					string_sql($basename) . " ,".
					string_sql($thumbfile). " ,". 
					string_sql(_CONST_TIMENOW) .
")");

if($id = $database->getInsertId()) {
	#删除临时文件
	@unlink(_CONST_UPLOAD_PATH . $_SESSION['step_4'][1]);
	@unlink(_CONST_UPLOAD_PATH . $_SESSION['step_4'][2]);
	#清除步骤session
	unset($_SESSION['step_1']);
	unset($_SESSION['step_2']);
	unset($_SESSION['step_3']);
	unset($_SESSION['step_4']);
	unset($_SESSION['token']);
	echo "<script>";
	echo "alert('创建投票成功！');";
	echo "window.location.href = '" . _CONST_WEB_URL . "vote.php?id=".$id."';";
	echo "</script>";
	exit;
}else{
	echo "<script>";
	echo "alert('创建投票失败!');";
	echo "window.location.href = '" . _CONST_WEB_URL . "index.php';";
	echo "</script>";
	exit;
}	

?>